<template>
  <div class="app-container">
    <div class="card-box">
      <el-button type="primary" @click="handleToAdd">新增</el-button>
    </div>
    <div class="card-box">
      <el-table
        v-loading="merchantStore.loading.list || merchantStore.loading.update"
        :data="merchantStore.list"
        row-id="id"
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="名称" prop="name" />
        <el-table-column label="用户">
          <template #default="{ row }">
            <el-tag type="info">{{ row.user.nickname }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="联系人" prop="contact_name" />
        <el-table-column label="商户状态">
          <template #default="{ row }">
            <el-tag v-if="(row.status as MerchantStatus) == 'active'" type="success">正常</el-tag>
            <el-tag v-if="(row.status as MerchantStatus) == 'approve'" type="warning">
              审核中
            </el-tag>
            <el-tag v-if="(row.status as MerchantStatus) == 'reject'" type="danger">
              审核被拒
            </el-tag>
            <el-tag v-if="(row.status as MerchantStatus) == 'locked'" type="info">锁定</el-tag>
            <el-tag v-if="(row.status as MerchantStatus) == 'deactive'">未营业</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="created_at" />
        <el-table-column label="商户地址" prop="address" />
        <el-table-column label="是否有效" prop="is_active">
          <template #default="{ row }">
            <el-tag :type="row.is_active ? 'success' : 'danger'">
              {{ row.is_active ? "有效" : "无效" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleToEdit(row)">编辑</el-button>
            <el-button
              v-if="(row as MerchantType).status == 'approve'"
              type="success"
              link
              @click="handleToExamine(row)"
            >
              审核
            </el-button>
            <el-popconfirm title="是否继续删除？" @confirm="handleDelete(row)">
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        style="margin-top: 10px"
        background
        :total="merchantStore.totalCount"
        @change="merchantStore.fetchMerchantList(params)"
      />
    </div>
    <Form
      v-model="visible"
      :type="formType"
      :form="currentForm"
      @confirm="merchantStore.fetchMerchantList(params)"
    />
    <el-dialog v-model="examineVisible" title="审核">
      <el-form>
        <el-form-item label="状态" prop="action">
          <el-radio-group v-model="currentForm.action">
            <el-radio value="pass">通过</el-radio>
            <el-radio value="reject">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="原因" prop="reason">
          <el-input
            v-model="currentForm.reason"
            placeholder="请输入原因"
            type="textarea"
            :rows="5"
            resize="none"
          />
        </el-form-item>
      </el-form>
      <div class="dialog-footer" style="display: flex; justify-content: flex-end">
        <el-button @click="examineVisible = false">取消</el-button>
        <el-button
          :loading="merchantStore.loading.examine"
          type="primary"
          @click="handleExamineConfirm"
        >
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {
  MerchantListQuery,
  MerchantStatus,
  MerchantType,
  MerchantForm,
} from "@/api/merchant/merchant";
import { useMerchantStoreHook } from "@/store/modules/merchant/merchant";
import Form, { type FormType } from "./components/form.vue";

const merchantStore = useMerchantStoreHook();

const visible = ref(false);

const examineVisible = ref(false);

const formType = ref<FormType>("add");

const currentForm = ref<MerchantForm>({
  name: "",
  address: "",
  province: 0,
  city: 0,
  district: 0,
  contact_name: "",
  contact_phone: "",
  store_image: "",
  referrer: 0,
  user: 0,
  is_active: false,
});

const params = reactive<MerchantListQuery>({
  page: 1,
  size: 10,
});

function handleToAdd() {
  formType.value = "add";
  visible.value = true;
}

async function handleDelete(row: MerchantType) {
  await merchantStore.fetchDeleteMerchant(row.id);
  await merchantStore.fetchMerchantList(params);
}

function handleToEdit(row: MerchantType) {
  formType.value = "edit";
  currentForm.value = {
    province: row.province.id,
    city: row.city.id,
    district: row.district.id,
    name: row.name,
    address: row.address,
    contact_name: row.contact_name,
    contact_phone: row.contact_phone,
    referrer: row.user.id,
    user: row.user.id,
    id: row.id,
    store_image: row.store_image,
    agent: row.agent?.id,
    is_active: row.is_active,
  };
  visible.value = true;
}

function handleToExamine(row: MerchantType) {
  currentForm.value = {
    is_active: row.is_active,
    province: row.province.id,
    city: row.city.id,
    district: row.district.id,
    name: row.name,
    address: row.address,
    contact_name: row.contact_name,
    contact_phone: row.contact_phone,
    referrer: row.user.id,
    user: row.user.id,
    id: row.id,
    store_image: row.store_image,
    agent: row.agent?.id,
    action: row.status,
  };
  examineVisible.value = true;
}

async function handleExamineConfirm() {
  await merchantStore.fetchExamineMerchant({
    id: currentForm.value.id!,
    action: currentForm.value.action,
    reason: currentForm.value.reason,
  });
  await merchantStore.fetchMerchantList(params);
  examineVisible.value = false;
}

onMounted(() => {
  merchantStore.fetchMerchantList(params);
});
</script>

<style scoped lang="scss"></style>
